import React from 'react';
import store from './store'
import { ADD_COUNT, GET_NEWS } from './store/actionType'
import axios from 'axios'
export default class Add extends React.Component {

  constructor (props) {
    super(props)
    this.state = store.getState()
  }

  componentDidMount () {
    store.subscribe(this.storeChange)
    this.loadNewsList()
  }

  async loadNewsList () {
    const { data: res } = await axios.get('http://115.159.87.220:4000/books')
    console.log(res)
    store.dispatch({
      type: GET_NEWS,
      value: res
    })
  }

  storeChange = (e) => {
    console.log(123)
    this.setState({
      ...store.getState()
    })
  }

  addCount = () => {
    store.dispatch({
      type: ADD_COUNT,
      value: 2
    })
  }

  render () {
    return (
      <div>
        { this.state.count }
        <button onClick={ this.addCount }>+1</button>
      </div>
    )
  }

}